package GuiLayer;
import ControlLayer.CtrOffer;
import ControlLayer.CtrOrder;
import ModelLayer.Offer;
import ModelLayer.Order;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.Action;

public class GUIOrder extends JFrame {

    private JPanel contentPane;
    private JTextField idInsert;
    private JTextArea offerText;
    private final Action action = new SwingAction();
    private JButton btnNewButton_1;
    private final Action action_2 = new SwingAction_2();
    private JButton btnNewButton_2;
    private final Action action_3 = new SwingAction_3();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUIOrder frame = new GUIOrder();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public GUIOrder() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 616, 427);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel lblId = new JLabel("id");

        idInsert = new JTextField();
        idInsert.setColumns(10);

        offerText = new JTextArea();

        JButton btnGo = new JButton("Go");
        btnGo.setAction(action);

        btnNewButton_1 = new JButton("New button");
        btnNewButton_1.setAction(action_2);

        btnNewButton_2 = new JButton("New button");
        btnNewButton_2.setAction(action_3);
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
        	gl_contentPane.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_contentPane.createSequentialGroup()
        			.addComponent(lblId)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(idInsert, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(btnGo)
        			.addPreferredGap(ComponentPlacement.RELATED, 207, Short.MAX_VALUE)
        			.addComponent(btnNewButton_2)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(btnNewButton_1))
        		.addComponent(offerText, GroupLayout.PREFERRED_SIZE, 297, GroupLayout.PREFERRED_SIZE)
        );
        gl_contentPane.setVerticalGroup(
        	gl_contentPane.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_contentPane.createSequentialGroup()
        			.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblId)
        				.addComponent(idInsert, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(btnGo)
        				.addComponent(btnNewButton_1)
        				.addComponent(btnNewButton_2))
        			.addPreferredGap(ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
        			.addComponent(offerText, GroupLayout.PREFERRED_SIZE, 254, GroupLayout.PREFERRED_SIZE))
        );
        contentPane.setLayout(gl_contentPane);
    }

    private class SwingAction extends AbstractAction
    {
        public SwingAction()
        {
            putValue(NAME, "Find Order");
            putValue(SHORT_DESCRIPTION, "Some short description");
        }
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                findOrderId();
            }
            catch(Exception ex)
            {
                System.out.println("Order could not be found");
            }
        }
    }


    private class SwingAction_2 extends AbstractAction
    {
        public SwingAction_2()
        {
            putValue(NAME, "EDIT");
            putValue(SHORT_DESCRIPTION, "Some short description");
        }
        public void actionPerformed(ActionEvent e)
        {

        }
    }

    private class SwingAction_3 extends AbstractAction
    {
        public SwingAction_3()
        {
            putValue(NAME, "Delete");
            putValue(SHORT_DESCRIPTION, "Some short description");
        }
        public void actionPerformed(ActionEvent e)
        {
            try {
                deleteOrder();
            }
            catch (Exception ex) {
                System.out.println("Order could not be deleted");
            }
        }
    }


    public void findOrderId() throws Exception
    {
        offerText.setText("");
        String id = idInsert.getText();
        System.out.println("idOrder : " + id);
        CtrOrder ctrlOrd = new CtrOrder();
        Order ord = ctrlOrd.findOrderId(id);

        String idOffer = ord.getOffer().getIdOffer();
        String createdDate = ord.getCreatedDate();
        String paymentDeadline = ord.getPaymentDeadline();
        String statusOrder = ord.getStatusOrder();

        CtrOffer ctrlOff = new CtrOffer();
        Offer off = ctrlOff.findOfferId(idOffer);

        double calculatedPrice = off.getCalculatedPrice();
        String dateStart = off.getDateStart();
        String dateEnd = off.getDateEnd();
        String acceptDeadline = off.getAcceptDeadline();
        double discount = off.getDiscount();
        String statusOffer = off.getStatusOffer();
        String sentDate = off.getSentDate();
        double pricePerHour = off.getPricePerHour();

        /**
         * If all the below is true the id is not in the table.
         */

        /*
        if (createdDate == null && paymentDeadline == null && statusOrder == null)
        {
            offerText.setText("");
        }
        else{
        */
        offerText.insert(
                "IdOffer = " + idOffer + "\n" +
                        "CreatedDate = " + createdDate + "\n" +
                        "PaymentDeadline = " + paymentDeadline + "\n" +
                        "StatusOrder = " + statusOrder + "\n" + "\n" +
                        " Offer information: \n" +
                        "CalculatedPrice = " + calculatedPrice + "\n" +
                        "Start Date = " + dateStart + "\n" +
                        "End Date = " + dateEnd + "\n" +
                        "Accepted DeadLine = " + acceptDeadline + "\n" +
                        "Discount = " + discount + "\n" +
                        "Offer Status = " + statusOffer + "\n" +
                        "Sent Date = " + sentDate + "\n" +
                        "Price per hour = " + pricePerHour, 0);
    }

    public void deleteOrder() throws Exception {
        String id = idInsert.getText();

        CtrOrder ctrOrd = new CtrOrder();
        ctrOrd.deleteOrder(id);
        System.out.println("Order deleted");
    }
}

